Round A APAC Test 2016 Problem C. gCampus 最短路

Problem C. gCampus

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
10 points
Large input
15 points
Download C-large-practice.in
your output file:
source file(s):    not needed for the practice contest
  

Problem

Company G has a main campus with N offices (numbered from 0 to N - 1) and Mbidirectional roads (numbered from 0 to M - 1). The ith road connects a pair of offices (Ui, Vi), and it takes Ci minutes to travel on it (in either direction).

A path between two offices X and Y is a series of one or more roads that starts at X and ends at Y. The time taken to travel a path is the sum of the times needed to travel each of the roads that make up the path. (It's guaranteed that there is at least one path connecting any two offices.)

Company G specializes in efficient transport solutions, but the CEO has just realized that, embarrassingly enough, its own road network may be suboptimal! She wants to know which roads in the campus are inefficient. A road is inefficient if and only if it is not included in any shortest paths between any offices.

Given the graph of offices and roads, can you help the CEO find all of the inefficient roads?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each case begins with one line with two integers N and M, indicating the number of offices and roads. This is followed by M lines containing three integers each: UiVi and Ci, indicating the ith road is between office Ui and office Vi, and it takes Ci minutes to travel on it.

Output

For each test case, output one line containing "Case #x:", where x is the test case number (starting from 1). Then output the road numbers of all of the inefficient roads, in increasing order, each on its own line. (Note that road 0 refers to the first road listed in a test case, road 1 refers to the second road, etc.)

Limits

0 < Ci ≤ 1000000.

Small dataset

1 ≤ T ≤ 10.
1 ≤ N = M ≤ 100.

Large dataset

1 ≤ T ≤ 3.
1 ≤ N ≤ 100.
1 ≤ M ≤ 10000.

Sample


Input 
 

Output 
 
2
3 3
0 1 10
1 2 3
2 0 3
3 3
0 1 10
1 2 3
2 1 3
Case #1:
0
Case #2:
題意,給出一個圖,要求,輸出非最短路上的所有邊。

直接用disjtra算法求出所有源點的最短路,如果,某個點的最短距離加上這條邊正好是別一個邊的最短距離,則這條邊就是在最短路上的邊,用了n次disjtra複雜度爲o(n * n * log(n))

#define typec ll
const typec inf = 1e18;
#define E 100005
#define V 105
typec cost[E],dist[V];
int e,pnt[E],nxt[E],head[V],prev[V],vis[V],num[V],index[E];
bool Use[E];
struct qnode {
    int v,en,k;typec c;
    qnode (int vv=0,int ene = 0,typec cc=0):v(vv),en(ene),c(cc){
    }
    bool operator <(const qnode & r)const {return c>r.c;}
};
priority_queue<qnode> que;
void dijkstra(int n,const int src){
    qnode mv;
    int i,j,k,pre;
    while(!que.empty()) que.pop();
    vis[src]=1;dist[src]=0;
    que.push(qnode(src,-1,0));
    for(pre=src,i=1;i<n;i++){
        for(j=head[pre];j!=-1;j=nxt[j]){
            k=pnt[j];
            if(vis[k]==0&&
               dist[pre]+cost[j]<dist[k]){
                    dist[k]=dist[pre]+cost[j];
                    que.push(qnode(pnt[j],j,dist[k]));
                    prev[k]=pre;
               }
        }
        while(!que.empty()&&vis[que.top().v]==1){
            que.pop();
        }
        if(que.empty())break;
        mv=que.top();que.pop();
        vis[pre=mv.v]=1;
    }
    for(int i = 1;i<=n;i++){
        pre = i;
        for(j=head[pre];j!=-1;j=nxt[j]){
            k=pnt[j];
            if(dist[pre] + cost[j] == dist[k]){
                Use[index[j]] = true;
            }
        }
    }
}
inline void addedge(int u,int v,typec c,int in){
    index[e] = in;pnt[e]=v;cost[e]=c;nxt[e]=head[u];head[u]=e++;
}


int T,n,m;
int main()
{
    freopen("C-large-practice.in", "r", stdin);
    freopen("C-large-practice.out", "w", stdout);

     while(S(T)!=EOF)
    {
        For(ta,1,T+1){
            S2(n,m);
            e=0;
            memset(head,-1,sizeof(head));
            fill(Use,false);
            for(int i=0;i<m;i++){
                int s,ee,c;
                scanf("%d%d%d",&s,&ee,&c);
                s++,ee++;
                addedge(s,ee,c,i);
                addedge(ee,s,c,i);
            }
            For(i,1,n+1){
                memset(vis,0,sizeof(vis));
                memset(prev,-1,sizeof(prev));
                for(int j=0;j<=n;j++)dist[j]=inf;
                dijkstra(n,i);
            }
            printf("Case #%d:\n",ta);
            FI(m){
                if(!Use[i]){
                    printf("%d\n",i);
                }
            }
        }

    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

發佈了463 篇原創文章 · 獲贊 57 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章