2015 ACM/ICPC North America Qualifier J. Torn to Pieces (spfa最短路)
作者:互联网
这道题我去年naq之前写过,但是没写出来,因为要记录路径什么的,当时的我啥也不会,今年又要naq了,我这次上来仔细重读了一下题,不就是个弱化版的记录路径spfa么,这有何难?
记录路径,费用流吼啊,行输入怎么办?stringstream吼啊
我说怎么这么多队过,原来是大水题hhh
#include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; #define limit (200000 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-6 #define FASTIO ios::sync_with_stdio(false);cin.tie(0); #define ff(a) printf("%d\n",a ); #define pi(a,b) pair<a,b> #define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i) #define MOD 998244353 #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next) #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin) #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout) #define debug(x) cout<<x<<endl typedef long long ll; typedef unsigned long long ull; char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf; inline ll read(){ #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) ll sign = 1, x = 0;char s = getchar(); while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();} while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();} return x * sign; #undef getchar }//快读 void print(ll x) { if(x/ 10) print(x / 10); *O++=x % 10+'0'; } void write(ll x) { if(x < 0)putchar('-'),x = -x; print(x); fwrite(obuf,O-obuf,1,stdout); O = obuf; } int n,m,vs,ve; int head[limit],cnt; int dist[limit],fa[limit],vis[limit],pre[limit]; struct node{ int to, next, flow, w; }edge[limit<<1]; void add_one(int u, int v, int flow, int w){ edge[cnt].to = v; edge[cnt].flow = flow; edge[cnt].w = w; edge[cnt].next = head[u]; head[u] = cnt++; } void add(int u, int v, int flow ,int w){ add_one(u,v,flow,w); add_one(v,u,0,-w); } void init(int flag = 0){ if(flag){ memset(dist, INF, sizeof(dist)); memset(vis, 0 , sizeof(vis)); }else{ memset(head, -1, sizeof(head)); cnt = 0; } } int spfa(){ queue<int>q; init(1); dist[vs] = 0; vis[vs] = 1; q.push(vs); pre[ve] = -1; while (q.size()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to, w = edge[i].w; int stream = edge[i].flow; if(dist[u] + w < dist[v] && stream > 0){ dist[v] = dist[u] + w; pre[v] = i; fa[v] = u; if(!vis[v]){ vis[v] = 1; q.push(v); } } } } return ~pre[ve]; } int max_flow,min_cost; map<int,string>mp; map<string, int>id; stack<string>stk; void dinic(){ if(!spfa()){ cout<<"no route found"<<endl; }else{ for(int i = ve; i != vs; i = fa[i]){ stk.push(mp[i]); } stk.push(mp[vs]); while (stk.size()){ cout<<stk.top()<<' '; stk.pop(); } } } void solve(){ cin>>n; init(); string str; getline(cin,str); int tot = 0; rep(_,1,n){ getline(cin,str); stringstream input(str); string st; input>>st; //cout<<st<<endl; if(!id[st])id[st] = ++tot,mp[tot]= st; string t; while (input>>t){ if(!id[t])id[t] = ++tot,mp[tot]= t; add(id[st],id[t],1,0); add(id[t],id[st],1,0); } } string s,t; cin>>s>>t; vs = id[s],ve = id[t]; dinic(); } int main() { #ifdef LOCAL FOPEN; //FOUT; #endif FASTIO //cin>>kase; //while (kase--) solve(); cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n"; return 0; }
标签:North,Qualifier,int,cin,Pieces,edge,dist,id,define 来源: https://www.cnblogs.com/tiany7/p/14356045.html