One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
After all the cows gathered at farm #X, they realized that every single cow left her party favors back at the farm. They decided to suspend the party and send all the cows back to get the party favors. The cows all travel optimal routes to their home farm and back to the party. What is the minimum number of time units the party must be suspended?One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
Line 1: Three space-separated integers, respectively: N, M, and X Lines 2..M+1: Line i+1 describes road i with three space-separated integers, respectively: Ai, Bi, and Ti. The described road connects Ai and Bi and requires Ti time units to traverse.
Line 1: One integer: the minimum amount of time the party must be suspended.
示例1
复制
4 8 2 1 2 7 1 3 8 1 4 4 2 1 3 2 3 1 3 1 2 3 4 6 4 2 2
复制
6
Four cows; eight roads; party at farm 2. Direct paths connects farm 2 to each of the other farms (to farm 1: 7 and 3; to farm 3: 1; to farm 4: 2). The longest path is length 3, so the round-trip time is 6.
AC代码:
- #include
- #include
- #include
- #include
- using namespace std;
-
- const int N=1010,M=200010;
-
- int n,m,x;
- int h[N],w[M],ne[M],e[M],idx;
- int dist[N];
- bool st[N];
-
- void add(int a,int b,int c)
- {
- w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
- }
-
- void spfa()
- {
- memset(dist,0x3f,sizeof dist);
- dist[x]=0;
- queue<int>q;
- q.push(x);
-
- while(q.size())
- {
- int t=q.front();
- q.pop();
- st[t]=0;
-
- for(int i=h[t];~i;i=ne[i])
- {
- int j=e[i];
- if(dist[j]>dist[t]+w[i])
- {
- dist[j]=dist[t]+w[i];
- if(!st[j])
- {
- st[j]=1;
- q.push(j);
- }
- }
- }
- }
- }
-
- int main()
- {
- memset(h,-1,sizeof h);
- scanf("%d%d%d",&n,&m,&x);
- while(m--)
- {
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- add(a,b,c),add(b,a,c);
- }
- spfa();
-
- int res=0;
- for(int i=1;i<=n;i++)
- res=max(res,dist[i]);
- cout<
2< - }
题目描述
One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
After all the cows gathered at farm #X, they realized that every single cow left her party favors back at the farm. They decided to suspend the party and send all the cows back to get the party favors. The cows all travel optimal routes to their home farm and back to the party. What is the minimum number of time units the party must be suspended?One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
输入描述:
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers, respectively: Ai, Bi, and Ti. The described road connects Ai and Bi and requires Ti time units to traverse.
输出描述:
Line 1: One integer: the minimum amount of time the party must be suspended.
示例1
输入
复制
4 8 2
1 2 7
1 3 8
1 4 4
2 1 3
2 3 1
3 1 2
3 4 6
4 2 2
输出
复制
6
说明
Four cows; eight roads; party at farm 2.
Direct paths connects farm 2 to each of the other farms (to farm 1: 7 and 3; to farm 3: 1; to farm 4: 2). The longest path is length 3, so the round-trip time is 6.